package com.volunteer.controller;

import com.volunteer.bean.LoginParam;
import com.volunteer.bean.PasswordParam;
import com.volunteer.model.User;
import com.volunteer.service.UserService;
import com.volunteer.util.Constants;
import com.volunteer.util.ResponseResult;
import com.volunteer.util.Result;
import com.volunteer.util.ServletUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static com.volunteer.util.ServletUtil.getSid;


@RestController
@RequestMapping("/users")  // 通过这里配置使下面的映射都在/users下
public class UserController {
    /**
     * 登陆接口
     */
    private final static String POST_USER_LOGIN = "/login";
    /**
     * 修改密码接口
     */
    private final static String PUT_USER_RESET_PASSWORD = "/resetPassword";
    /**
     * 退出接口
     */
    private final static String POST_USER_EXIT = "/exit";
    /**
     * 获取用户信息接口
     */
    private final static String GET_USER_INFO = "/info";
    /**
     * 更新用户信息接口
     */
    private final static String PUT_USER_UPDATE_INFO = "/updateInfo";
    /**
     * 用户注册接口
     */
    private final static String PUT_USER_REGISTER = "/register";
    // 异常打印
    private final static Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private ServletUtil servletUtil;

    @ApiOperation(value = "登陆接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "用户手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String")
    })
    @PostMapping(value = POST_USER_LOGIN)
    public Result userLogin(@RequestBody LoginParam loginParam) {
        return userService.userLogin(loginParam);
    }

    @ApiOperation(value = "通过旧密码修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, dataType = "String")
    })
    @PutMapping(value = PUT_USER_RESET_PASSWORD)
    public Result resetPassword(@RequestBody PasswordParam passwordParam, HttpServletRequest request) {
        try {
            Long userId = servletUtil.getUserIdbyRequestInRedis(request);
            return userService.resetPassword(passwordParam, userId);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            if (Constants.DEFAULT_FAIL_USER_NOT_LOGGED_IN.equals(e.getMessage()))
                return ResponseResult.genNotLogin();
            return ResponseResult.genFailResult(Constants.DEFAULT_FAIL_REQUEST_FAILED);
        }
    }

    @ApiOperation(value = "用户退出登入状态")
    @PostMapping(value = POST_USER_EXIT)
    public Result userExit(HttpServletRequest request) {
        try {
            String sid = getSid(request);
            return userService.userExit(sid);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return ResponseResult.genFailResult(Constants.DEFAULT_FAIL_REQUEST_FAILED);
        }
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping(value = GET_USER_INFO)
    public Result userInfo(HttpServletRequest request) {
        try {
            Long userId = servletUtil.getUserIdbyRequestInRedis(request);
            return userService.userInfo(userId);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            if (Constants.DEFAULT_FAIL_USER_NOT_LOGGED_IN.equals(e.getMessage()))
                return ResponseResult.genNotLogin();
            return ResponseResult.genFailResult(Constants.DEFAULT_FAIL_REQUEST_FAILED);
        }
    }

    @ApiOperation(value = "更改用户基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "用户手机号码", dataType = "Long"),
            @ApiImplicitParam(name = "birthday", value = "用户生日", dataType = "Date"),
            @ApiImplicitParam(name = "identityCard", value = "用户身份证号", dataType = "String"),
            @ApiImplicitParam(name = "address", value = "用户住址", dataType = "String"),
            @ApiImplicitParam(name = "birthpalce", value = "用户户籍", dataType = "String"),
            @ApiImplicitParam(name = "workAddress", value = "工作地址", dataType = "String"),
            @ApiImplicitParam(name = "qq", value = "QQ", dataType = "Integer"),
            @ApiImplicitParam(name = "wechat", value = "微信", dataType = "String")
    })
    @PutMapping(value = PUT_USER_UPDATE_INFO)
    public Result updateInfo(@RequestBody User user, HttpServletRequest request) {
        try {
            Long userId = servletUtil.getUserIdbyRequestInRedis(request);
            user.setId(userId);
            return userService.updateInfo(user);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            if (Constants.DEFAULT_FAIL_USER_NOT_LOGGED_IN.equals(e.getMessage()))
                return ResponseResult.genNotLogin();
            return ResponseResult.genFailResult(Constants.DEFAULT_FAIL_REQUEST_FAILED);
        }
    }

    @ApiOperation(value = "注册", notes = "只有管理员才可以为新成员注册账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", dataType = "String"),
            @ApiImplicitParam(name = "phone", value = "用户手机号码", dataType = "Long"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String")
    })
    @PutMapping(value = PUT_USER_REGISTER)
    public Result register(@RequestBody User user, HttpServletRequest request) {
        try {
            Long userId = servletUtil.getUserIdbyRequestInRedis(request);
            return userService.register(user, userId);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            if (Constants.DEFAULT_FAIL_USER_NOT_LOGGED_IN.equals(e.getMessage()))
                return ResponseResult.genNotLogin();
            return ResponseResult.genFailResult(Constants.DEFAULT_FAIL_REQUEST_FAILED);
        }
    }
}
